package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.pm.dto.RecruitmentAddDTO;
import com.oa.pm.dto.RecruitmentAuditDTO;
import com.oa.pm.dto.RecruitmentListDTO;
import com.oa.pm.entity.Recruitment;
import com.oa.pm.service.IRecruitmentService;
import com.oa.pm.vo.RecruitmentDetailsVO;
import com.oa.pm.vo.RecruitmentListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/recruitment")
@Api(description = "招聘申请表模块")
public class RecruitmentController extends BaseController {

    private final IRecruitmentService recruitmentService;

    @Autowired
    public RecruitmentController(IRecruitmentService recruitmentService) {
        this.recruitmentService = recruitmentService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp<PageResult<RecruitmentListVO>> list(RecruitmentListDTO recruitmentListDTO) {
        recruitmentListDTO.setUserId(getLoginUserId());
        return new Resp().success(recruitmentService.findRecruitmentList(recruitmentListDTO));
    }

    @GetMapping("/export")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public void export(RecruitmentListDTO recruitmentListDTO) {
        recruitmentListDTO.setUserId(getLoginUserId());
        recruitmentService.export(recruitmentListDTO, response);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<RecruitmentDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(recruitmentService.findRecruitmentById(id));
    }


    /**
     * 新增
     *
     * @param recruitmentAddDTO RecruitmentAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "招聘申请表对象") @RequestBody(required = false) RecruitmentAddDTO recruitmentAddDTO) throws DataIsEmptyException, BusinessException {
        recruitmentAddDTO.setProposer(getLoginUserId());
        return recruitmentService.add(recruitmentAddDTO);
    }

    @PutMapping("/audit")
    @CommonController
    @Description("审核")
    @ApiOperation(value = "审核")
    public Resp audit(@RequestBody RecruitmentAuditDTO recruitmentAuditDTO) throws DataIsEmptyException {
        return recruitmentService.audit(recruitmentAuditDTO);
    }

    /**
     * 根据Id修改
     *
     * @param recruitment Recruitment
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "招聘申请表对象") @RequestBody(required = false) Recruitment recruitment) {
        if (recruitmentService.updateById(recruitment)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Recruitment recruitment = recruitmentService.selectById(id);
        if (recruitment != null && recruitment.getIsDeleted() == 2) {
            recruitment.setIsDeleted(1);
            if (recruitmentService.updateById(recruitment)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
